/*
 *   This File is Part of CuteOgre Game Engine
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *	Copyright 2010 Alexandre Pires ( c.alexandre.pires@gmail.com )
 *  Created on: 2010/08/22
 */

#ifndef SCRIPTMANAGER_H_
#define SCRIPTMANAGER_H_

#include "IManager.h"
#include "Singleton.h"

#include <QScriptEngine>
#include <QScriptEngineDebugger>

class ScriptManager :
		public IManager,
		public Singleton<ScriptManager> {

	friend class GameEngine;

private:
	bool mShutdown;

protected:
	ScriptManager () :
		mShutdown ( true ) {
	}

public:
	~ScriptManager () {
	}

	static ScriptManager* getSingletonPtr ( void ) {
		return ms_Singleton;
	}

	static ScriptManager& getSingleton ( void ) {
		assert( ms_Singleton );
		return ( *ms_Singleton );
	}

	void setupEnvironment ( QScriptEngine* rEngine );
	bool loadScript ( const QString& rFile, QScriptEngine* rEngine, bool rPush = true );
	QScriptValue importScript ( const QString& rFile, QScriptEngine* rEngine );
	bool evaluateScript ( const QString& content, QScriptEngine* rEngine, bool rPush = true );

	inline static void setup ( QScriptEngine* rEngine ) {
		ScriptManager::getSingleton().setupEnvironment( rEngine );
	}

	inline static bool load ( const QString& rFile, QScriptEngine* rEngine, bool rPush = true ) {
		return ScriptManager::getSingleton().loadScript( rFile, rEngine, rPush );
	}

	inline static QScriptValue import ( const QString& rFile, QScriptEngine* rEngine ) {
		return ScriptManager::getSingleton().importScript( rFile, rEngine );
	}

	inline static bool evaluate ( const QString& rFile, QScriptEngine* rEngine, bool rPush = true ) {
		return ScriptManager::getSingleton().evaluateScript( rFile, rEngine, rPush );
	}

	void init ();
	void shutdown ();

private:
	QString readFileContents ( const QString& rFile );
};

#endif /* SCRIPTMANAGER_H_ */
